package edu.chl.da.ejb.core;

import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * This class respresent a airport.
 * @author Jakob
 */
@Entity
@Table(name = "AIRPORT")
@NamedQueries({
    @NamedQuery(name = "Airport.findAll", query = "SELECT a FROM Airport a"),
    @NamedQuery(name = "Airport.findByName", query = "SELECT a FROM Airport a WHERE a.airportPK.name = :name"),
    @NamedQuery(name = "Airport.findByDestination", query = "SELECT a FROM Airport a WHERE a.airportPK.destination = :destination")})
public class Airport implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected AirportPK airportPK;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "airport")
    private List<Flight> flightList;
    @JoinColumn(name = "DESTINATION", referencedColumnName = "NAME", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Destination destination1;

    public Airport() {
    }

    public Airport(AirportPK airportPK) {
        this.airportPK = airportPK;
    }

    public Airport(String name, String destination) {
        this.airportPK = new AirportPK(name, destination);
    }

    public AirportPK getAirportPK() {
        return airportPK;
    }

    public void setAirportPK(AirportPK airportPK) {
        this.airportPK = airportPK;
    }

    public List<Flight> getFlightList() {
        return flightList;
    }

    public void setFlightList(List<Flight> flightList) {
        this.flightList = flightList;
    }

    public Destination getDestination1() {
        return destination1;
    }

    public void setDestination1(Destination destination1) {
        this.destination1 = destination1;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (airportPK != null ? airportPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Airport)) {
            return false;
        }
        Airport other = (Airport) object;
        if ((this.airportPK == null && other.airportPK != null) || (this.airportPK != null && !this.airportPK.equals(other.airportPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.da.ejb.core.Airport[airportPK=" + airportPK + "]";
    }

}
